package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.Vo.ResultVo;
import com.qf.Vo.ResultVoUtils;
import com.qf.enums.ResultEnum;
import com.qf.exeception.EntryExeception;
import com.qf.pojo.EbookCategory;
import com.qf.pojo.EbookEntry;
import com.qf.service.EntryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/index")
public class EntryController {

    @Autowired
    EntryService entryService;



//    跳转到展示页面
    @GetMapping("/list")
    public String list(String name, @RequestParam(value = "page",defaultValue = "1")Integer page,
                       @RequestParam(value = "size",defaultValue = "5")Integer size, Model model){


        PageInfo<EbookEntry> pageInfo = entryService.findByPage(page, size, name);

        model.addAttribute("pageInfo", pageInfo);


        //下拉菜单列表
        List<EbookCategory> categoryList = entryService.findAllCategory();
        model.addAttribute("categoryList", categoryList);
        return "index";

    }

    //跳转到添加页面
    @GetMapping("/add-ui")
    public String addUI(){

        return "add";
    }

    //ajax校验参数
    @PostMapping("/checkblank")
    @ResponseBody
    public ResultVo checkblank(@Valid EbookEntry ebookEntry,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            String message = bindingResult.getFieldError().getDefaultMessage();

            throw new EntryExeception(ResultEnum.PARAM_ERROR.getCode(),message);
        }

        Integer integer = entryService.saveData(ebookEntry);
        if (integer!=1){
            throw new EntryExeception(ResultEnum.SAVE_ERROR);
        }

        return ResultVoUtils.success();
    }



    @GetMapping("/update-ui")
    public String updateUI(Integer id,Model model){
        EbookEntry entry = entryService.findById(id);
        model.addAttribute("id", id);
        model.addAttribute("entry", entry);
        return "update";
    }

    @PostMapping("/update")
    public String update(@Valid EbookEntry ebookEntry,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            String message = bindingResult.getFieldError().getDefaultMessage();
            throw new EntryExeception(ResultEnum.PARAM_ERROR.getCode(),message);
        }

        Integer integer = entryService.updateData(ebookEntry);
        if (integer!=1){
            throw new EntryExeception(ResultEnum.UPDATE_ERROR);
        }

        return "index";

    }

    @GetMapping("/delete")
    public String delete(Integer id){
        Integer integer = entryService.deleteById(id);
        if (integer!=1){
            throw new EntryExeception(ResultEnum.DELETE_ERROR);
        }
        return "redirect:/index/list";
    }



//    public String update(@Valid String title, @Valid Date created,BindingResult bindingResult,
//                         String summary, String uploaduser,Integer id){
//
//        if (bindingResult.hasErrors()){
//            throw new EntryExeception(ResultEnum.PARAM_ERROR);
//        }
//
//       Integer count= entryService.updateData(id,title,summary,uploaduser,created);
//        if (count!=1){
//            throw new EntryExeception(ResultEnum.UPDATE_ERROR);
//        }
//
//
//        return "index";
//    }




}
